<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="../../css/reset.css" />
    <link rel="stylesheet" type="text/css" href="../../css/register.css" />
</head>

<body>
    <div class="pos">
        <div class="login">
            <span class="close iconfont icon-close"></span>
            <img src="../../img/login/loginlogo.png" alt="">
            <ul>
                <li></li>
                <li class="phone">使用手机号登陆</li>
                <li></li>
            </ul>
            <!-- <form action="" id="form"> -->
                <input style="margin-top: 0;" type="text" name="tel" id="username" placeholder="请输入您的手机号">
                
                <input type="password" name="pass" id="pass" placeholder="请输入密码">
               
                <input type="password" name="pass2" id="pass2" placeholder="请再次输入密码">
                <span class="tips"></span>
                <div class="clear"></div>
                <button class="loginbtn">注册</button>
            <!-- </form> -->
            
            <p>
                <input type="checkbox" class="xuanze"/>
                <span><a href="javascript:;">我同意 《用户使用协议》</a></span>
                <span>
                    已有账号
                    <a href="javascript:;">去登陆</a>
                </span>
            </p>
        </div>
    </div>
</body>
</html>
<script src="../../common/jquery-3.4.1.min.js"></script>
<script src="../../common/myAjax.js"></script>
<script>
    $('.loginbtn').click(async function(){
        let username = $('#username').val();
        let password = $('#pass').val();
        let password2 = $('#pass2').val();
        
        if(!username || !password || !password2){
            $('.tips').html('用户名或密码不能为空');
            // return;
        }
        //手机号正则判断
        let reg1 = /^1[3-9][0-9]{9}$/;
        if(!reg1.test($('#username').val())){
            $('.tips').html('手机号输入有误');
            // return;
        }
        //密码正则判断
        let reg2 = /(?!^[0-9]{6,15}$)(?!^[a-zA-Z]{6,15}$)(^[0-9a-zA-Z]{6,15}$)/;
        if(!reg2.test($('#pass').val())){
            $('.tips').html('密码必须是6-15位数字字母组合');
            // return;
        }
        if(password != password2){
            $('.tips').html('两次密码不一致，请重新输入');
            // return;
        }
         console.log($('#pass').val(),$('#pass2').val(),$('#username').val());
        //往后台传
        let [err,result] = await myAjax({
            url:'/api/register',
            type:'post',
            data:{
                username: $('#username').val(),
                password: $('#pass').val(),
                password2:$('#pass2').val()
            }
        })
       
      
        let {msg} = result;
        if(result.code == 200){
            alert(`${msg}`);
            $('.tips').html('');
            location.href = './login.html';
        } 
        console.log(result);
        if(result.code == 400){
           alert('用户名已注册！')
        }
       
    })
</script>